# replication plots
library(tidyverse)
library(rio)
library(naniar)
library(scales)
library(dvmisc)
library(ggpubr)
library(gridExtra)

setwd("C:/Users/quossfa/Dropbox/NIMBY/replication_files")

# Figure A.1 ####
w5 <- import("tabA7_A8_figA1_A2_replication_data.dta")
w5 <- w5 %>% replace_with_na_all(condition = ~.x %in% c(-33, -44, -55, -66, -77, -88, -97, -99,
                                                        -9, -8))
labels_q8 <- rev(c("very\nmuch more", 
"much more", "more", "the same\namount", "less", "much less", "very\nmuch less", 
"mobile phone\nmasts do\nnot affect me"))

# gender: q2
labels_q2 <- c("Female", "Male")
q8_sex <- ggplot(w5, aes(w5_q8, fill = factor(w5_q2))) +
  geom_bar(aes(y = (..count..)/sum(..count..)),
           position = "dodge") +
  scale_x_continuous(labels = labels_q8, breaks = c(1:8)) +
  scale_y_continuous(labels = label_percent(accuracy = 1)) +
  scale_fill_manual("Respondent Sex", values = c("#1F407A", "#E69F00"),
                    labels = labels_q2) +
  labs(x = "Health impact of 5G vs 3G/4G",
       y = "percent of respondents",
       caption = "n = 1920, not in same treatment arm as survey experiment") + 
  theme_bw()+ theme(legend.position="bottom")

# age: 
w5$age_group <- NA
w5$age_group <-  quant_groups(2020-w5$w5_q1, 3)
labels_q1 <- c("18-44", "45-60", "61+")

q8_age <- ggplot(w5, aes(w5_q8, fill = factor(age_group))) +
  geom_bar(aes(y = (..count..)/sum(..count..)),
           position = "dodge") +
  scale_x_continuous(labels = labels_q8, breaks = c(1:8)) +
  scale_y_continuous(labels = label_percent(accuracy = 1)) +
  scale_fill_manual("Respondent Age", values = c("#1F407A", "#E69F00", "grey"),
                    labels = labels_q1) +
  labs(x = "Health impact of 5G vs 3G/4G",
       y = "percent of respondents",
       #title = "Worry by Age",
       caption = "n = 1920, not in same treatment arm as survey experiment") + 
  theme_bw()+ theme(legend.position="bottom")


ggarrange(q8_sex, q8_age)
# save:
# ggsave("A1.png", 
#       width = 11, height = 5, dpi = 400)

# Figure A.2 ####
labels_q10 <- rev(c("far too\nstrongly", 
                    "too strongly", "rather too\nstrongly", 
                    "just right", "rather too\nweakly", 
                    "too weakly", "far too\nweakly"))

# gender: q2
labels_q2 <- c("Female", "Male")
q10_sex <- ggplot(w5, aes(w5_q10, fill = factor(w5_q2))) +
  geom_bar(aes(y = (..count..)/sum(..count..)),
           position = "dodge") +
  scale_x_continuous(labels = labels_q10, breaks = c(1:7)) +
  scale_y_continuous(labels = label_percent(accuracy = 1)) +
  scale_fill_manual("Respondent Sex", values = c("#1F407A", "#E69F00"),
                    labels = labels_q2) +
  labs(x = "Protection of Swiss population", # from radiation by Swiss government",
       y = "percent of respondents",
       caption = "n = 1779, not in same treatment arm as survey experiment") + 
  theme_bw()+ theme(legend.position="bottom")


# age: 
w5$age_group <- NA
w5$age_group <-  quant_groups(2020-w5$w5_q1, 3)
labels_q1 <- c("18-44", "45-60", "61+")

q10_age <- ggplot(w5, aes(w5_q10, fill = factor(age_group))) +
  geom_bar(aes(y = (..count..)/sum(..count..)),
           position = "dodge") +
  scale_x_continuous(labels = labels_q10, breaks = c(1:7)) +
  scale_y_continuous(labels = label_percent(accuracy = 1)) +
  scale_fill_manual("Respondent Age", values = c("#1F407A", "#E69F00", "grey"),
                    labels = labels_q1) +
  labs(x = "Protection of Swiss population",# from radiation by Swiss government",
       y = "percent of respondents",
       #title = "Worry by Age",
       caption = "n = 1779, not in same treatment arm as survey experiment") + 
  theme_bw()+ theme(legend.position="bottom")

cor(w5$w5_q1, w5$w5_q10, use = "pairwise.complete") %>% round(.,2)
cor(w5$w5_q1[w5$w5_q10>1], w5$w5_q10[w5$w5_q10>1], use = "pairwise.complete") %>% round(.,2)

ggarrange(q10_sex, q10_age)
# save
# ggsave("A2.png", width = 11, height = 5, dpi = 400)


# plot A.3, B.2 ####

# only for respondents
data_coord_df <- import("nimby_replication_data.dta")

# only relevant variables
data_coord_df <- data_coord_df[, c("PubId","treat1_300m", "treat2_300m",
                                   "treat1_500m", "treat2_500m",
                                   "treat1_rect", "treat2_rect")] 
# 300 m

# treat 2:

treat2_300m <- ggplot(data_coord_df, aes(treat2_300m)) + geom_bar() +
  labs(x = "Number of 3G/4G masts", y = "number of respondents",
       title = "3G/4G masts in 300 m distance of respondents") + theme_minimal()

# treat 1:

treat1_300m <- ggplot(data_coord_df, aes(treat1_300m)) + geom_bar() +
  labs(x = "Number of 5G masts", y = "number of respondents",
       title = "5G masts in 300 m distance of respondents") + theme_minimal()

# 500 m

# treat 2:
treat2_500m <- ggplot(data_coord_df, aes(treat2_500m)) + geom_bar() +
  labs(x = "Number of 3G/4G masts", y = "number of respondents",
       title = "3G/4G masts in 500 m distance of respondents")+ theme_minimal()

# treat 1:
treat1_500m <- ggplot(data_coord_df, aes(treat1_500m)) + geom_bar() +
  labs(x = "Number of 5G masts", y = "number of respondents",
       title = "5G masts in 500 m distance of respondents")+ theme_minimal()

# on map

# treat 2:
treat2_rect <- ggplot(data_coord_df, aes(treat2_rect)) + geom_bar() +
  labs(x = "Number of 3G/4G masts", y = "number of respondents",
       title = "3G/4G masts seen on map")+ theme_minimal()

# treat 1:
treat1_rect <- ggplot(data_coord_df, aes(treat1_rect)) + geom_bar() +
  labs(x = "Number of 5G masts", y = "number of respondents",
       title = "5G masts seen on map")+ theme_minimal()

# save plot
# png("maps_treatment_overview.png",
#     width = 23, height = 20, units = "cm", res = 200)
# grid.arrange(treat2_300m, treat1_300m, 
#              treat2_500m, treat1_500m,
#              treat2_rect, treat1_rect,
#              ncol=2)
# dev.off()

